package com.srd.phone.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shengrongdai.srd.R;
import com.srd.phone.activity.CreditsExchangeActivity;
import com.srd.phone.bean.ChildData;
import com.srd.phone.bean.GroupData;
import com.srd.phone.bean.MyAward;
import com.srd.phone.utils.views.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class MyAwardAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private List<GroupData> mList = new ArrayList<>(10);
    private int mGrade = 0;

    public MyAwardAdapter(Context context, List<GroupData> list){
        this.mContext = context;
        this.mList = list;
    }

    public void setDataSoucre(MyAward myAward){
        this.mList = myAward.list;
        mGrade = myAward.grade;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public GroupData getGroup(int groupPosition) {
        GroupData groupData = null;
        try{
            groupData = mList.get(groupPosition);
        }catch (Exception e){
            groupData = null;
        }
        return groupData;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<ChildData> items = getGroup(groupPosition).items;
        return items.size();
    }

    @Override
    public ChildData getChild(int groupPosition, int childPosition) {
        List<ChildData> items = getGroup(groupPosition).items;
        return items.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View view, ViewGroup parent) {

        ViewHolder viewHolder =  ViewHolder.get(mContext, view, parent, R.layout.layout_group_item, groupPosition);
        RelativeLayout rl_parent_layout = viewHolder.getView(R.id.rl_parent_layout);
        TextView tv_group_name = viewHolder.getView(R.id.tv_group_name);
        ImageView iv_right_icon = viewHolder.getView(R.id.iv_right_icon);

        GroupData groupData = getGroup(groupPosition);
        tv_group_name.setText(groupData.name);
        if(isExpanded){
            rl_parent_layout.setBackgroundColor(Color.parseColor("#F7F7F7"));
            iv_right_icon.setBackgroundResource(R.mipmap.icon_down);
        }
        else{
            rl_parent_layout.setBackgroundColor(Color.parseColor("#FFFFFF"));
            iv_right_icon.setBackgroundResource(R.mipmap.icon_right);
        }
        return viewHolder.getConvertView();
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition,  boolean isLastChild, View view, ViewGroup parent) {

        final ChildData childData = getChild(groupPosition, childPosition);
        final GroupData groupData = getGroup(groupPosition);

        ViewHolder viewHolder = ViewHolder.get(mContext, view, parent, R.layout.layout_childs_item, childPosition);
        LinearLayout ll_layout_1 = viewHolder.getView(R.id.ll_layout_1);
        LinearLayout ll_layout_2 = viewHolder.getView(R.id.ll_layout_2);

        if(groupData.state == 4){
            ll_layout_1.setVisibility(View.GONE);
            ll_layout_2.setVisibility(View.VISIBLE);

            ListView mListView = viewHolder.getView(R.id.listview);
            mListView.setAdapter(new AwardIntegralAdapter(mContext, childData.integralList));

            viewHolder.getView(R.id.tv_credits_exchange).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mContext.startActivity(new Intent(mContext, CreditsExchangeActivity.class));
                }
            });

            TextView tv_integral_count = viewHolder.getView(R.id.tv_integral_count);
            tv_integral_count.setText("您目前的积分是" + mGrade + "积分");
        }else{

            ll_layout_1.setVisibility(View.VISIBLE);
            ll_layout_2.setVisibility(View.GONE);

            TextView tv_child_name = viewHolder.getView(R.id.tv_child_name);
            TextView tv_child_date = viewHolder.getView(R.id.tv_child_date);
            ImageView iv_icon = viewHolder.getView(R.id.iv_icon);
            tv_child_name.setText(childData.money);
            tv_child_date.setText( "有效期限 " + childData.ctime + " - " + childData.stime);

            if(groupData.state == 1){  // 红包
                if(childData.state == 72)
                    iv_icon.setBackgroundResource(R.mipmap.bg_award_red);
                else if(childData.state == 73)
                    iv_icon.setBackgroundResource(R.mipmap.bg_award_used);
                else
                    iv_icon.setBackgroundResource(R.mipmap.bg_award_past_due);
            }else if(groupData.state == 2){  // 体验劵
                if(childData.state == 72)
                    iv_icon.setBackgroundResource(R.mipmap.bg_award_yellow);
                else if(childData.state == 73)
                    iv_icon.setBackgroundResource(R.mipmap.bg_award_used);
                else
                    iv_icon.setBackgroundResource(R.mipmap.bg_award_past_due);
            }else if(groupData.state == 3){  // 加息劵
                if(childData.state == 72)
                    iv_icon.setBackgroundResource(R.mipmap.bg_award_green);
                else if(childData.state == 73)
                    iv_icon.setBackgroundResource(R.mipmap.bg_award_used);
                else
                    iv_icon.setBackgroundResource(R.mipmap.bg_award_past_due);
            }
        }
        return viewHolder.getConvertView();
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}
